/**
 * Created by Celebration-Grey on 2017/5/10.
 */

weibo.info = {};

weibo.info.$iframe = null;

weibo.info.$table = null;

weibo.info.$form = null;

weibo.info.init = function () {
    weibo.profile.topNav();
    weibo.profile.photo();
    weibo.info.$iframe = window.frames[0];
    if(localStorage.getItem('user_info') != null){
        userInfo = localStorage.getItem('user_info');
        userInfo = $.parseJSON(userInfo);
        var u_nickname = userInfo['user_nickname'];
        u_nickname = weibo.public.htmlEncodeByRegExp(u_nickname);
        $('.user-nickname').html(u_nickname);
        weibo.info.getUserInfo(weibo.info.getUserInfoSuccess,weibo.info.getUserInfoErro);
    }
    else{
        alert('请先登录！');
        window.location.href = 'login.html';
    }
};

weibo.info.getUserInfo = function (fnSuccess,fnError) {
  $.ajax({
      type : 'GET',
      url : host + 'weibo_getSelfInfo',
      data : {},
      xhrFields: {
          withCredentials: true
      },
      success : fnSuccess,
      error : fnError
  });
};

weibo.info.getUserInfoSuccess = function (data) {
    if(data.status == 200){
        weibo.profile.setUserInfo(data.data);
        weibo.profile.headChange();
        weibo.profile.upCover();
        weibo.profile.upBackground();
        setTimeout(function(){
          weibo.info.$iframe.postMessage(data.data,window.location.origin);
        },500);
    }
    else{
        $.Huimodalalert(data.message,2000);
    }
};

weibo.info.fillDataInTable = function (data) {
    var $tbody = $('<tbody></tbody>');
    for(var key in data){
        var $tr = $('<tr class="'+key+'"><td class="">'+key+'</td><td>'+data[key]+'</td><tr>');
        $tbody.append($tr);
    }
    weibo.info.$table.append($tbody);
};

weibo.info.fillDataInForm = function (data) {

};

weibo.info.getUserInfoError = function (XMLHttpRequest, textStatus, errorThrown) {
    console.log(XMLHttpRequest);
    console.log(textStatus);
    console.log(errorThrown);
};